<template>
  <el-dialog v-model="visible" :title="show ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="105px" @keyup.enter="submitHandle()">
      <div class="item_line">
        <el-form-item prop="id" label="商品ID">
          <el-input v-model="dataForm.id" placeholder="商品ID"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="orderIdx">
          <el-input v-model="dataForm.orderIdx" placeholder="排序" type="number"></el-input>
        </el-form-item>
      </div>
      <div class="item_line">
        <el-form-item label="商品类型" prop="goodsType">
          <ma-dict-select
            v-model="dataForm.goodsType"
            dict-type="goods_type"
            clearable
            placeholder="商品类型"></ma-dict-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
        </el-form-item>
      </div>
      <div class="item_line">
        <el-form-item label="商品小标题" prop="goodsSubject">
          <el-input v-model="dataForm.goodsSubject" placeholder="商品小标题"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="goodsDetail">
          <el-input v-model="dataForm.goodsDetail" placeholder="商品描述"></el-input>
        </el-form-item>
      </div>
      <div class="item_line">
        <el-form-item label="金额" prop="amount">
          <el-input v-model="dataForm.amount" placeholder="金额"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="dataForm.status" placeholder="状态"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="参数" prop="params">
        <el-input v-model="dataForm.params" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElFormItem, ElInput, ElMessage } from 'element-plus'
import { getCommodityInfoApi, useCommoditySubmitApi } from '@/api/gps/orders'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const dataFormRef = ref()
const show = ref(true)
const dataForm = reactive({
  id: '',
  goodsSubject: '',
  goodsType: undefined,
  goodsDetail: '',
  status: '',
  goodsName: '',
  amount: '',
  params: undefined,
  orderIdx: ''
})

function getCommodityInfo(id: number) {
  getCommodityInfoApi(id).then((res) => {
    Object.assign(dataForm, res.data)
    dataForm.params = JSON.stringify(dataForm.params)
  })
}

const init = (id?: number, orgId?: string) => {
  show.value = true
  visible.value = true
  dataForm.id = ''

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  if (id) {
    show.value = false
    getCommodityInfo(id)
  }
}

const dataRules = ref({
  id: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  goodsType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  goodsName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  goodsSubject: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  goodsDetail: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  amount: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
  if(dataForm.params){
    dataForm.params = JSON.parse(dataForm.params)
  }
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    useCommoditySubmitApi(dataForm).then(() => {
      ElMessage.success({
        message: '操作成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>

<style scoped>
.item_line {
  display: flex;
}
.item_line > div {
  flex: 1;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
